﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;

namespace LsTool
{
    public class UrlCom
    { 
      /// <summary>
      /// 用于完整编码
      /// </summary>
      /// <param name="dictionary">
      /// The dictionary.
      /// </param>
      /// <param name="urlencode">大写</param>
      /// <returns>
      /// The <see cref="string"/>.
      /// </returns>
        public static string GetUrlencode(IEnumerable<KeyValuePair<string, string>> dictionary, bool urlencode = true)
        {
      
            var dy = dictionary.ToList().OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);

            List<string> strings = new List<string>();
            foreach (var item in dy)
            {
                if (item.Key==null)
                {
                    continue; 
                }
                var val = item.Value;
                if (urlencode)
                {
                    val = UrlencodeToUpper(item.Value);
                }
                strings.Add(item.Key + "=" + val);
            }
            string join = string.Join("&", strings);

            return join;
        }
        /// <summary>
        /// 用于完整编码
        /// </summary>
        /// <param name="nvc">
        /// The dictionary.
        /// </param>
        /// <param name="daxie">大写</param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetUrlencode(NameValueCollection nvc)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            if (nvc==null)
            {
                return string.Empty;
            }
            foreach (var item in nvc.AllKeys)
            {
                if (string.IsNullOrWhiteSpace(item))
                {
                    continue;
                }
                list.Add(new KeyValuePair<string, string>(item, nvc[item]));
            }
            return GetUrlencode(list, false);
        }


        /// <summary>
        /// URL转议
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string StringToUrlEncode(string url)
        {
            return System.Web.HttpUtility.UrlEncode(url);
        } 
        /// <summary>
        /// URL转议
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlEncodeToString(string url)
        {
            return System.Web.HttpUtility.UrlDecode(url);
        }

        /// <summary>
        /// url转义（大写）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="daxie">大写</param>
        /// <returns></returns>
        public static string UrlencodeToUpper(string str, bool daxie = true)
        {
            var sb = new StringBuilder();
            foreach (var c in str)
            {
                string urlEncode = HttpUtility.UrlEncode(c.ToString());
                if (urlEncode != null && urlEncode.Length > 1)
                {
                    var ue = daxie ? urlEncode.ToUpper() : urlEncode.ToLower();
                    sb.Append(ue);
                }
                else
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }
    }
}